package com.example.rabbitmq.demo2_deadQueue;

import com.example.rabbitmq.enums.ExchangeEnum;
import com.example.rabbitmq.enums.RabbitmqQueueNamesEnum;
import com.example.rabbitmq.enums.RoutingKeyEnum;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author songyahui
 * @Date 2023/7/11 11:51
 * @Description 模拟消息转到死信队列的情况，由Consumer2消费死信队列的消息
 */
public class Consumer2_2_Dead {

    public static void consumeMessageWithDirect(Channel channel) throws IOException {
        //声明死信和普通交换机 类型未direct
        channel.exchangeDeclare(ExchangeEnum.DEAD_EXCHANGE.name, BuiltinExchangeType.DIRECT);
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("死信消费："+new String(message.getBody(), "UTF-8"));
        };
        //声明死信队列
        channel.queueDeclare(RabbitmqQueueNamesEnum.QUEUE_DEAD.queueName, false, false, false, null);
        //绑定死信交换机和队列
        channel.queueBind(RabbitmqQueueNamesEnum.QUEUE_DEAD.queueName, ExchangeEnum.DEAD_EXCHANGE.name, RoutingKeyEnum.DEAD.name());
        //绑定死信交换机和队列
        channel.basicConsume(RabbitmqQueueNamesEnum.QUEUE_DEAD.queueName, true, deliverCallback, consumerTag -> {});
    }
}
